package com.scada.model.result;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.servlet.ModelAndView;


public class ActionResult implements Serializable {

	/**
	 * Serial Version UID
	 */
	private static final long serialVersionUID = -2581960680481338269L;

	private static final String KEY_STATUS = "success";
	private static final String KEY_MESSAGES = "messages";
	private static final String KEY_MSG = "msg";

	/**
	 * status
	 */
	private boolean success = false;
	/**
	 * Messages
	 */
	private List<String> messages = new ArrayList<String>();
	
	private String msg="";

	/**
	 * Attachment message
	 */
	private Map<String, Object> attachs = new LinkedHashMap<String, Object>();

	public boolean getSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public List<String> getMessages() {
		return messages;
	}

	public void setMessages(List<String> messages) {
		this.messages = messages;
	}

	public void addMessage(String message) {
		this.messages.add(message);
	}

	public void attach(String key, Object value) {
		attachs.put(key, value);
	}

	public void attachMap(Map<String, Object> map) {
		attachs.putAll(map);
	}

	public Map<String, Object> getAttachs() {
		return attachs;
	}
	
	public ModelAndView getJsonModelView() {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put(KEY_STATUS, success);
		model.put(KEY_MESSAGES, messages);
		model.put(KEY_MSG, msg);

		for (String key : attachs.keySet()) {
			Object value = attachs.get(key);
			model.put(key, value);
		}
		return new ModelAndView("jsonView", model);
	}
	
	public String getJsonString(){
		
		StringBuffer stb=new StringBuffer("{");
		stb.append("\""+KEY_STATUS+"\":"+success+"");
		if(messages!=null && messages.size()>0)
		{
			stb.append(",\""+KEY_MESSAGES+"\":\"");
			for(String str:messages)
			{
				stb.append(str+"  ");
			}
			stb.append("\"");
		}
		stb.append("}");
		return stb.toString();
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	
}
